package com.bankapp.service.bank;

import com.bankapp.domain.bank.Bank;
import com.bankapp.domain.bank.CheckingAccount;
import com.bankapp.domain.bank.Client;
import com.bankapp.domain.bank.Gender;
import com.bankapp.exception.bank.BankException;
import com.bankapp.exception.bank.OverDraftLimitExceededException;
import com.bankapp.network.bank.BankServer;
import com.bankapp.network.bank.ServerStub;

import java.io.IOException;

public class BankApplication {
    public static void main(String[] args) {
        String pathToFeedFile = "Feed.txt";
        String pathToBankFile = "Bank.ser";
        String server = "";

        for (int i = 0; i < args.length; i += 2) {
            switch (args[i]) {
                case "-loadfeed":
                    pathToFeedFile = args[i + 1];
                    break;
                case "-loadbank":
                    pathToBankFile = args[i + 1];
                    break;
                case "-server":
                    server = "";
                    break;
                default:
                    System.out.println("Uncorrect parammeters! Try again!");

            }
        }


        Bank bank = new Bank();
        BankService bankService = new BankService();
        CheckingAccount checkingAccount = new CheckingAccount(700);
        bankService.addClient(bank, "Mario", "Smith", Gender.MALE, checkingAccount, "Mario.Sm@db.com");
        bankService.addClient(bank, "Mari", "Smith", Gender.FEMALE, checkingAccount, "Mari.Sm@db.com");

        BankServer bankServer = new BankServer(new ServerStub());
        bankServer.startServer(bank);

        bankService.saveBank(bank, pathToBankFile);
        bank = bankService.readBank(pathToBankFile);

        Client[] clientsArray = bank.getClientsArray();
        Client firstClient = clientsArray[0];
        Client secondClient = clientsArray[1];
        boolean compareByLink = firstClient == secondClient;
        boolean compareByValue = firstClient.equals(secondClient);
        System.out.println("Compare two the same elements by link: " + compareByLink);
        System.out.println("Compare two the same elements by value: " + compareByValue);

        firstClient.getAccount().setBalance(100);
        secondClient.getAccount().setBalance(50);

        try {
            firstClient.getAccount().withdraw(20);
        } catch (OverDraftLimitExceededException e) {
            System.out.println("Not enough money on account " + e.getAccount() + ". Maximum amount of withdraws on this account is " + e.getBalanceAndOverdraft() + ".");
        } catch (BankException e) {
            e.printStackTrace();
        }

        bankService.addClient(bank, "Maggie", "Smith", Gender.FEMALE, checkingAccount, "Maggie.Sm@db.com");

        try {
            bank.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
